<?php
/**
 * Created by PhpStorm.
 * User: AE
 * Date: 2019/9/20
 * Time: 11:09
 */

namespace app\api\controller;

use think\Controller;
use app\common\model\Pjdomain as PjdomainModel;
use app\common\model\Wxurls as WxurlsModel;

class Tj extends Controller
{

    public function manyurls($id){
        $this->_id = $id;
        $wxurls = new WxurlsModel();
        //  根据id挑出域名后缀
        $url_postfix = $wxurls->query('SELECT postfix,pid FROM think_wxurls WHERE id =  ' . $id );
        $url_postfix = $url_postfix[0];
//        var_dump($url_postfix);
        if($url_postfix){
            // 选出随机的二跳域名domain  SELECT * FROM think_wxurls WHERE  is_delete = 1 AND `status` = 1 AND type = 2  AND pid = 3
            $url_domain = $wxurls->query('SELECT domain, id  FROM think_wxurls WHERE is_delete = 1 AND `status` = 1 AND type = 2  AND pid = '.$url_postfix['pid'].' ORDER BY RAND() LIMIT 1');
            if($url_domain){
                // 增加一次的访问量  update student set score=score+1 where id = 1
                $wxurls->query('UPDATE think_wxurls set jump_reads=jump_reads+1 WHERE id =' . $url_domain[0]['id'] . ' OR id = '. $id);
                $url_domain = $url_domain[0]['domain'];
                $domain =  $url_domain.'/'.$url_postfix['postfix'] .'/' ;
                return json(['status' => 1, 'url' => $domain]);
            }else{
                // 域名被封完了, 就直接返回后缀, 接抗封老域名
                return json(['status' => 2, 'url' => $url_postfix['postfix'].'/']);
            }
        }else{
            // 找不到对应的id
            return json(['status' => 3, 'msg' => 'ID is not here!']);
        }
    }
}

